package com.adams.action.sys;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.adams.access.db.bean.Parameter;
import com.adams.action.view.ViewAction;
import com.adams.common.BaseAction;
import com.adams.common.Constants;
import com.adams.custom.page.mgt.service.ViewParseService;
import com.adams.log.service.OptLogService;
import com.adams.sys.parameter.service.ParameterService;

/**
 * 
 * 
 * File Name : ParameterAction.java
 * 
 * @Description : 参数管理类
 * @author 902990封文进
 */
public class ParameterAction extends BaseAction
{

	private ViewParseService viewParseService;

	private ParameterService parameterService;

	private OptLogService optLogService;

	private int menuId;

	private String gridPanelId;

	private String viewEventId;

	private String formPanelId;

	private String viewItemId;

	private String searchPanelId;

	/**
	 * 
	 * Description : 显示布局
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String listPanel() throws Exception
	{
		// Map map = viewParseService.getParseViewByMenuId(getUserId(), menuId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseViewJSONByMenuId(getUserId(), menuId));
		return null;
	}

	/**
	 * Description : 显示查询条字段信息
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	public String listSearchItem() throws Exception
	{
		// Map map = viewParseService.getParseSearchItems(getUserId(), menuId,
		// searchPanelId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseSearchItemsJSON(getUserId(), menuId,
				searchPanelId));
		return null;
	}

	/**
	 * 
	 * Description : 显示元素
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	public String listGridStructure() throws Exception
	{
		// Map map = viewParseService.getParseGrids(getUserId(), gridPanelId);
		// write(JSONObject.fromObject(map).toString());
		write(viewParseService.getParseGridsJSON(getUserId(), gridPanelId));
		return null;
	}

	/**
	 * 
	 * Description : 显示数据
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String listGridData() throws Exception
	{
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map map = viewParseService.getParseGridDatas(getUserId(), gridPanelId,
				searchPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());
		return null;
	}

	/**
	 * 
	 * Description : 打开面板窗口
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String openPanelWindow() throws Exception
	{

		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		write(JSONObject.fromObject(map).toString());

		return null;

	}

	/**
	 * 
	 * Description : 添加参数
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String add() throws Exception
	{

		Map inputMap = ViewAction.requestParameterMap2Map(request);

		String parameterName = (String) inputMap.get("parameterName");
		String parameterType = (String) inputMap.get("parameterType");

		Parameter parameter = parameterService.queryByTypeAndName(
				parameterType, parameterName);
		if (parameter != null)
		{
			sendFailureMessage(Constants.EXIST_PARAMETER_NAME);
		} else
		{
			Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
					menuId, formPanelId, inputMap);
			write(JSONObject.fromObject(map).toString());

			// 记录日志
			createLog("addParameter", "parameter name:" + parameterName);
		}

		return null;
	}

	/**
	 * 
	 * Description : 更新参数
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String update() throws Exception
	{

		Map inputMap = ViewAction.requestParameterMap2Map(request);

		// String id = (String) inputMap.get("id");
		String parameterName = (String) inputMap.get("parameterName");
		String parameterType = (String) inputMap.get("parameterType");

		Parameter parameter = parameterService.queryByTypeAndName(
				parameterType, parameterName);
		if (parameter != null)
		{
			Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
					menuId, formPanelId, inputMap);
			write(JSONObject.fromObject(map).toString());

			// 记录日志
			createLog("updateParameter", "parameter name:" + parameterName);
		} else
		{
			sendFailureMessage(Constants.EXIST_PARAMETER_NAME);
		}

		return null;
	}

	/**
	 * 
	 * Description : 删除参数
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String delete() throws Exception
	{
		List<Map> mapList = ViewAction.getDataMap(request);
		viewParseService
				.deleteDataByViewForm(getUserId(), viewEventId, mapList);

		// 记录日志
		StringBuilder parameterNames = new StringBuilder();
		for (Map map : mapList)
		{
			parameterNames.append(map.get("parameterName") + ",");
		}
		createLog("deleteParameter", "parameter name:"
				+ parameterNames.substring(0, parameterNames.length() - 1));

		sendSuccessMessage(Constants.DELETE_SUCCESS);
		return null;
	}

	/**
	 * Description : 得到参数详细数据
	 * 
	 * @return String
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String get() throws Exception
	{

		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getDataByViewForm(getUserId(), menuId,
				formPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());

		return null;
	}

	// /////////////////////属性设置/////////////////////////

	public void setMenuId(int menuId)
	{
		this.menuId = menuId;
	}

	public void setViewParseService(ViewParseService viewParseService)
	{
		this.viewParseService = viewParseService;
	}

	public void setParameterService(ParameterService parameterService)
	{
		this.parameterService = parameterService;
	}

	public void setOptLogService(OptLogService optLogService)
	{
		this.optLogService = optLogService;
	}

	public void setViewEventId(String viewEventId)
	{
		this.viewEventId = viewEventId;
	}

	public void setViewItemId(String viewItemId)
	{
		this.viewItemId = viewItemId;
	}

	public void setFormPanelId(String formPanelId)
	{
		this.formPanelId = formPanelId;
	}

	public void setGridPanelId(String gridPanelId)
	{
		this.gridPanelId = gridPanelId;
	}

	public void setSearchPanelId(String searchPanelId)
	{
		this.searchPanelId = searchPanelId;
	}
}
